<?php
class DynamicEDOHelper
{
	static private $typedata;
	
	
	
	
	/**
	 * Builds typedata.
	 *
	 */
	public function getTypeData(EDO_object_dynamic $obj)
	{
		
		if(!isset(self::$typedata[$obj->getClass()]))
		{
		
			$typedata = array();
			
			$refle = new ReflectionClass($obj);
			$statics = $refle->getStaticProperties();
													
			$typedata['typeid'] = $obj->_typeid;
			$typedata['tables'] = array();
				
			$typedata['extended_datatypes'] = array();
	        $typedata['relations'] = array();
	        
	        $typedata['fields_raw'] = array();
			
	        $typedata['fields_tables'] = array();
	        
			do
			{
				// $statics = $refle->getStaticProperties();
				
				
        		$typedata['relations_classes'] = array();
				               
				
				if(isset($obj->_relations))
				{
					foreach($obj->_relations as $key => &$relation)
					{
						$typedata['relations'][$relation['name']] = $relation;
						$typedata['relations_classes'][$relation['name']] = $obj->getClass();
					}	
				}
			    			
				if(isset($obj->_extended_datatypes))
				{				
					
					foreach($obj->_extended_datatypes as $key => $value)
					{
						$typedata['extended_datatypes'][$key] = $value;
					}
				}
				
			
					
				$tables = array_reverse($obj->_tables);
	
				
				
				foreach($tables as $tablename)
				{
					array_unshift($typedata['tables'], $tablename);
				}
				$refle = $refle->getParentClass();
	
			} while ($refle->getName() != 'EDO_object_basic');
			
			foreach($typedata['tables'] as $tablekey => $tablename)
			{
				$table = $obj->ldb->getTable($tablename);

				$columns = $table->getColumns();
				
				foreach($columns as &$table_field)
				{
                    $typedata['tables_fields'][$tablekey][] = $table_field['name'];
	
					$typedata['fields_tables'][$table_field['name']] = $tablename;
	
	
					if(isset($typedata['extended_datatypes'][$table_field['name']]))
					{
						$table_field['type'] = $typedata['extended_datatypes'][$table_field['name']];
					}
	
					$typedata['fields_raw'][$table_field['name']] = $table_field;
						
					
				}
	
	           $typedata['dimensional_tables'] = array();
    		}
	
    		
    		
    		$typedata['meta'] = self::getMetaData($this);
    			
			
		   		    			
	
			self::$typedata[$obj->getClass()] = $typedata;
			
		}
		
		
		return self::$typedata[$obj->getClass()];
	}


	
	/**
	 * Returns dynamic tables.
	 *
	 * @param LDB_base $db
	 * @return unknown
	 */
	static public function getTables(EDO_object_dynamic $obj)
	{
		static $tblstore = array();
		
		if(!isset($tblstore[$obj->getTypeId()]))
		{
			$phrase = "SELECT tablename FROM edo_dynamic_types_tables WHERE typeid = " . $obj->getTypeId() . " ORDER BY tableid";
			$result = $obj->ldb->getCol($phrase);
					
			if(!$result)
				$result = array();
			
			$tblstore[$obj->getTypeId()] = $result;
				
		}
				
		return $tblstore[$obj->getTypeId()];
		
	}
	
	/**
	 * Returns dynamic relations.
	 *
	 * @param LDB_base $db
	 * @return unknown
	 */
	static public function getRelations(EDO_object_dynamic $obj)
	{
		static $relstore;
		if(!isset($relstore[$obj->getTypeId()]))
		{
			
		    $phrase = "SELECT * FROM edo_dynamic_relation WHERE typeid = {$obj->getTypeId()}";
		    $rawRelations = $obj->ldb->getAll($phrase);
		    
		    $relations = array();
		    
		    foreach($rawRelations as &$rawRelation)
		    {
		        $relation = array();
		        $relation['name'] = $rawRelation['name'];
		        
		        $query = "SELECT keyid, valuestr FROM edo_dynamic_relationdata WHERE typeid = {$rawRelation['typeid']} AND name = '{$rawRelation['name']}'";
		        $relationData = $obj->ldb->getAll($query);
		        
		        foreach($relationData as &$relationDataRow)
		        {
		            $relation[$relationDataRow['keyid']] = $relationDataRow['valuestr'];
		        }
		        		        		        
		        $relations[] = $relation;
		    }
		    
		     
	    
		    
		    
		    /*
		    $phrase = "SELECT * FROM edo_dynamic_relations WHERE typeid = {$obj->getTypeId()}";
			$relations = $obj->ldb->getAll($phrase);
			
			
			
			foreach($relations as &$relation)
			{
				$phrase = "SELECT rsource, rtarget FROM edo_dynamic_relations_restrictions WHERE typeid = {$obj->getTypeId()} AND relationobject = '{$relation['relationobject']}' AND fieldname = '{$relation['fieldname']}'"; 
				$relation['restrictor'] = $obj->ldb->getRow($phrase);
			}
			*/
			
			$relstore[$obj->getTypeId()] = $relations;
		}
						
		return $relstore[$obj->getTypeId()];
		
	}

	/**
	 * Returns dynamic extended datatypes.
	 *
	 * @param LDB_base $db
	 * @return unknown
	 */
	static public function getExtendedDataTypes(EDO_object_dynamic $obj)
	{
		static $extstore = array();
		if(!isset($extstore[$obj->getTypeId()]))
		{
		
			$phrase = "SELECT fieldname, fieldtype FROM edo_dynamic_extended_datatypes WHERE typeid = " . $obj->getTypeId() . " ORDER BY fieldname";
			$result = $obj->ldb->getAll($phrase);
					
			if(!$result)
			{
				$result = array();
			}
			else 
			{
		
				$extended = array();
				foreach($result as &$row)
				{
					$extended[$row['fieldname']] = $row['fieldtype'];
				}
				$result = $extended;
			}
			
			$extstore[$obj->getTypeId()] = $result;
		}
		
		return $extstore[$obj->getTypeId()];
	}

	
	public static function getMetaData(EDO_object_dynamic $obj)
	{
   		static $metastore = array();
		if(!isset($metastore[$obj->getTypeId()]))
		{
	    
    	    $phrase = "SELECT keyid, valuestr FROM edo_dynamic_meta WHERE typeid = {$obj->getTypeid()}";
    	    $result = $obj->ldb->getAll($phrase);
    	    
    	    if(!$result)
    		{
    			$result = array();
    		}
    		else 
    		{
    			$meta = array();
    			foreach($result as &$row)
    			{
    				$meta[$row['keyid']] = $row['valuestr'];
    			}
    			$result = $meta;
    		}
    		$metastore[$obj->getTypeId()] = $result;
		}
		return $metastore[$obj->getTypeId()];
	}
	


	
	
	
}
?>